home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Util / Time / mbwatch.lha / mbwatch / src / mbwatch.c
C/C++ Source or Header  |  1995-05-26  |  12KB  |  454 lines

  1. /******************************************************************************
  2. ** Michael Bauer's Watch - © April 1995 by Michael Bauer
  3. ******************************************************************************/
  4.  
  5. /// "*** Includes ***"
  6. #include <proto/dos.h>
  7. #include <proto/exec.h>
  8. #include <proto/graphics.h>
  9. #include <proto/gadtools.h>
  10. #include <proto/timer.h>
  11. #include <proto/intuition.h>
  12. #include <proto/utility.h>
  13. #include <exec/libraries.h>
  14. #include <exec/memory.h>
  15. #include <graphics/gfxbase.h>
  16. #include <graphics/gfx.h>
  17. #include <graphics/gfxmacros.h>
  18. #include <devices/timer.h>
  19. #include <libraries/gadtools.h>
  20. #include <utility/date.h>
  21. #include <intuition/intuitionbase.h>
  22. #include <dos/rdargs.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <lib/rexx.h>
  27. #include <dos/dos.h>
  28. #include <easysound.h>
  29. ///
  30. /// "*** Variablen ***"
  31. const char Versiontag[] = "$VER: MBWatch 1.1 (25.6.95)";
  32.  
  33. extern struct Library       *UtilityBase;
  34. extern struct IntuitionBase *IntuitionBase;
  35. extern struct GfxBase       *GfxBase;
  36. extern struct Library       *GadToolsBase;
  37.  
  38. char *RexxHostName = "MBWATCH";
  39.  
  40. char ArgStr[] = "X=XPOS/N,Y=YPOS/N,SCREEN/K,S=START/S,LOGTIME/N,SEC/N,MIN/N,HOUR/N,ALARM/N,SAMPLE/K,LOGFILE/S";
  41. LONG ArgArray[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  42.  
  43. ULONG signal;
  44. int running = FALSE, StartMe = FALSE, done = FALSE;
  45. static struct timeval time_val0, time_val1;
  46. UBYTE t_string[32];
  47. ULONG secoffset = 0, xpos = 0, ypos = 0;
  48.  
  49. struct Library      *TimerBase;
  50. struct MsgPort      *TimerMP;
  51. struct timerequest  *TimerIO;
  52.  
  53. char *PubScreenName = NULL;
  54. char *foofile = NULL;
  55. char soundfile[100];
  56.  
  57. struct timerequest *tr;
  58. struct Window *w;
  59. struct ClockData clockdata0, clockdata1, clockoffset;
  60. struct RDArgs *rda = NULL;
  61. int outReq = 0, alarm = 0, alarmtimer;
  62. int foo = 0, logtime = 60, min,sec,hour, minoff = 0, secoff = 0, houroff = 0;
  63. int wantlog = 0;
  64. ///
  65. /// "*** Prototypes ***"
  66. static void DisposeTimerIO(int);
  67. int InitTimerIO(void);
  68. int SendTimerIO(int, ULONG);
  69. struct Window *InitWindow(void);
  70. void _main(void);
  71. long DoRexxCommand(void *, struct MsgPort *, char *, char **);
  72. int text_width(char *);
  73. void ParseArgs(void);
  74. void HandleIDCMP(void);
  75. void CheckFormerTime(void);
  76. ///
  77. /// "DisposeTimerIO"
  78. static void DisposeTimerIO (int n) {
  79.  
  80.     switch (n) {
  81.         case 0:
  82.             CloseDevice ((struct IORequest *) TimerIO);
  83.         case 1:
  84.             DeleteIORequest (TimerIO);
  85.         case 2:
  86.             DeleteMsgPort (TimerMP);
  87.         default:
  88.             break;
  89.     }
  90.     remove ("s:.mbwatch");
  91. }
  92. ///
  93. /// "InitTimerIO"
  94. int InitTimerIO (void) {
  95.  
  96.     TimerMP = CreateMsgPort();
  97.     if (!TimerMP)
  98.         DisposeTimerIO(3);
  99.  
  100.     TimerIO = CreateIORequest(TimerMP, sizeof(struct timerequest));
  101.     if (!TimerIO)
  102.         DisposeTimerIO(2);
  103.  
  104.     if (OpenDevice(TIMERNAME, UNIT_VBLANK, (struct IORequest *)TimerIO, 0L) != 0L)
  105.         DisposeTimerIO(1);
  106.  
  107.     TimerBase = (struct Library *)TimerIO->tr_node.io_Device;
  108.  
  109.     return (TRUE);
  110. }
  111. ///
  112. /// "SendTimerIO"
  113. int SendTimerIO (int type, ULONG secs) {
  114.  
  115.     struct timerequest *tr;
  116.  
  117.     if (tr = (struct timerequest *)AllocMem(sizeof (struct timerequest), MEMF_PUBLIC)) {
  118.         *tr = *TimerIO;
  119.  
  120.         tr->tr_node.io_Command  = type;
  121.         tr->tr_time.tv_secs     = secs;
  122.         tr->tr_time.tv_micro    = 50000L;
  123.  
  124.         SendIO (&tr->tr_node);
  125.  
  126.         return (1);
  127.     }
  128.     return (0);
  129. }
  130. ///
  131. /// "InitWindow"
  132. struct Window *InitWindow (void) {
  133.  
  134.     struct Screen *screen;
  135.     struct Window *window;
  136.     int height, width;
  137.  
  138.     if (screen = LockPubScreen (PubScreenName)) {
  139.     if (!screen)
  140.         screen = LockPubScreen (NULL);
  141.  
  142.         width = text_width("00:00:00") + 70;
  143.         height = screen->BarHeight + 1;
  144.  
  145.         window = OpenWindowTags(NULL,
  146.             WA_Left, xpos,
  147.             WA_Top,  ypos,
  148.             WA_Title,"00:00:00",
  149.             WA_Flags, (ULONG)WFLG_CLOSEGADGET|WFLG_DEPTHGADGET|WFLG_DRAGBAR,
  150.             WA_IDCMP, (ULONG)IDCMP_CLOSEWINDOW,
  151.             WA_Height, (ULONG)height,
  152.             WA_Width, (ULONG)width,
  153.             WA_PubScreen, screen,
  154.             WA_PubScreenFallBack, TRUE,
  155.             TAG_DONE);
  156.  
  157.         if (!window)
  158.             exit(20);
  159.  
  160.         UnlockPubScreen (NULL, screen);
  161.         return (window);
  162.     }
  163. }
  164. ///
  165. /// "DoRexxCommand"
  166. long DoRexxCommand(void *msg, struct MsgPort *port, char *arg0, char **pres) {
  167.  
  168.     int rc;
  169.  
  170.     if (stricmp(arg0, "start") == 0) {
  171.         running = TRUE;
  172.         return;
  173.     }
  174.  
  175.     if (stricmp(arg0, "stop") == 0) {
  176.         running = FALSE;
  177.         return;
  178.     }
  179.  
  180.     if (stricmp(arg0, "gettime") == 0) {
  181.         *pres = t_string;
  182.         rc = 0;
  183.         return (rc);
  184.     }
  185.  
  186.     if (stricmp(arg0, "reset") == 0) {
  187.         time_val0 = time_val1 = TimerIO->tr_time;
  188.         SubTime(&time_val1,&time_val0);
  189.         secoffset = 0;
  190.         return;
  191.     }
  192.  
  193.     if (stricmp(arg0, "quit") == 0) {
  194.         done = TRUE;
  195.         return;
  196.     }
  197.  
  198.     return (0);
  199. }
  200. ///
  201. /// "text_width"
  202. int text_width(char *str) {
  203.  
  204.     struct IntuiText itext;
  205.     itext.FrontPen  = 1;
  206.     itext.BackPen   = 0;
  207.     itext.DrawMode  = JAM1;
  208.     itext.LeftEdge  = 0;
  209.     itext.TopEdge   = 1;
  210.     itext.ITextFont = NULL;
  211.     itext.NextText  = NULL;
  212.     itext.IText     = str;
  213.  
  214.     return( IntuiTextLength(&itext) );
  215. }
  216. ///
  217. /// "ParseArgs"
  218. void ParseArgs(void) {
  219.     rda = ReadArgs(ArgStr, ArgArray, rda);
  220.  
  221.     if (ArgArray[0])
  222.         xpos = *(LONG *)ArgArray[0];
  223.  
  224.     if (ArgArray[1])
  225.         ypos = *(LONG *)ArgArray[1];
  226.  
  227.     if (ArgArray[2]) {
  228.         PubScreenName = (STRPTR)ArgArray[2];
  229.         printf("%s\n",PubScreenName);
  230.     }
  231.     else
  232.         PubScreenName = NULL;
  233.  
  234.     if (ArgArray[3]) {
  235.         StartMe = TRUE;
  236.         running = TRUE;
  237.     }
  238.  
  239.     if (ArgArray[4])
  240.         logtime = *(LONG *)ArgArray[4];
  241.  
  242.     if (ArgArray[5])
  243.         secoff = *(LONG *)ArgArray[5];
  244.  
  245.     if (ArgArray[6])
  246.         minoff = *(LONG *)ArgArray[6];
  247.  
  248.     if (ArgArray[7])
  249.         houroff = *(LONG *)ArgArray[7];
  250.  
  251.     if (ArgArray[8])
  252.         alarm = *(LONG *)ArgArray[8];
  253.  
  254.     if (ArgArray[9]) {
  255.         foofile = (STRPTR)ArgArray[9];
  256.         strcpy(soundfile, foofile);
  257.     }
  258.  
  259.     if (ArgArray[10])
  260.         wantlog = 1;
  261.  
  262.     FreeArgs(rda);
  263. }
  264. ///
  265. /// "HandleIDCMP"
  266. void HandleIDCMP(void) {
  267.     struct IntuiMessage *imsg;
  268.     ULONG im_code, im_class;
  269.     APTR im_address;
  270.  
  271.     while (imsg = GT_GetIMsg (w->UserPort)) {
  272.  
  273.         im_class = imsg->Class;
  274.         im_code = imsg->Code;
  275.         im_address = imsg->IAddress;
  276.  
  277.         GT_ReplyIMsg (imsg);
  278.             switch (im_class) {
  279.  
  280.                 case CLOSEWINDOW:
  281.                     done = TRUE;
  282.                     break;
  283.                 default:
  284.                     break;
  285.         }
  286.     }
  287. }
  288. ///
  289. /// "CheckFormerTime"
  290. void CheckFormerTime(void) {
  291.     FILE *file;
  292.  
  293.     /**
  294.     *** Open s:.mbwatch and read the data if the file exists, set the data to
  295.     *** 0 if the file doesn't exist.
  296.     **/
  297.     file = fopen("s:.mbwatch","r");
  298.     if (file) {
  299.         fscanf(file,"%d:%d:%d",&hour,&min,&sec);
  300.         fclose(file);
  301.     }
  302.     else {
  303.         hour = 0;
  304.         min = 0;
  305.         sec = 0;
  306.     }
  307.  
  308.     /**
  309.     *** Fill in the clockdata structure
  310.     **/
  311.     clockoffset.sec = sec + secoff;
  312.     clockoffset.min = min + minoff;
  313.     clockoffset.hour = hour + houroff;
  314.     clockoffset.mday = 1;
  315.     clockoffset.month = 1;
  316.     clockoffset.year = 1995;
  317.     clockoffset.wday = 1;
  318.  
  319.     /**
  320.     *** Calculate the seconds from the given data
  321.     **/
  322.     secoffset = Date2Amiga(&clockoffset);
  323. }
  324. ///
  325. /// "_main"
  326. void _main (void) {
  327.  
  328.     ParseArgs();        // Pass Arguments
  329.     if (wantlog == 1)
  330.         CheckFormerTime();  // Check if there's a s:.mbwatch file
  331.  
  332.     if (w = InitWindow ()) {
  333.  
  334.         if (InitTimerIO ()) {
  335.  
  336.             outReq += SendTimerIO (TR_ADDREQUEST, 0L);
  337.  
  338.             while (!done || outReq) {
  339.  
  340.                 signal = Wait ( 1L << TimerMP->mp_SigBit | SIGBREAKF_CTRL_C |
  341.                                 1L << w->UserPort->mp_SigBit | 1L << RexxSigBit);
  342.  
  343.                 TimerIO->tr_node.io_Command = TR_GETSYSTIME;
  344.                 DoIO (&TimerIO->tr_node);
  345.  
  346.                 /**
  347.                 *** StartMe ist nur am Anfang wahr -> Zeit wird auf 00:00:00
  348.                 *** gesetzt.
  349.                 **/
  350.                 if (running == FALSE || StartMe == TRUE) {
  351.                     struct timeval tvdiff = TimerIO->tr_time;
  352.                     SubTime (&tvdiff, &time_val0);
  353.                     SubTime (&tvdiff, &time_val1);
  354.                     AddTime (&time_val0, &tvdiff);
  355.                     StartMe = FALSE;
  356.                 }
  357.  
  358.                 time_val1 = TimerIO->tr_time;
  359.                 SubTime (&time_val1, &time_val0);
  360.  
  361.                 /**
  362.                 *** Handle CTRL-C
  363.                 **/
  364.                 if ((signal & SIGBREAKF_CTRL_C) == SIGBREAKF_CTRL_C)
  365.                     done = TRUE;
  366.  
  367.                 /**
  368.                 *** Handle Incoming Rexx Commands
  369.                 **/
  370.                 if (signal & (1 << RexxSigBit))
  371.                     ProcessRexxCommands(NULL);
  372.  
  373.                 /**
  374.                 *** Handle IDCMP Messages
  375.                 **/
  376.                 if (signal & (1L << w->UserPort->mp_SigBit))
  377.                     HandleIDCMP();
  378.  
  379.                 /**
  380.                 *** Timer Event handling
  381.                 **/
  382.                 if (signal & (1L << TimerMP->mp_SigBit)) {
  383.  
  384.                     static ULONG oldTSec = 0, oldSSec = 0;
  385.  
  386.                     while (tr = (struct timerequest *) GetMsg(TimerMP)) {
  387.                         outReq--;
  388.                         FreeMem ((UBYTE *) tr, sizeof (struct timerequest));
  389.  
  390.                         if (oldTSec < TimerIO->tr_time.tv_secs || oldSSec < time_val1.tv_secs) {
  391.                             oldSSec = time_val1.tv_secs;
  392.                             oldTSec = TimerIO->tr_time.tv_secs;
  393.  
  394.                             Amiga2Date (time_val1.tv_secs + secoffset, &clockdata1);
  395.                             sprintf (t_string, "%02d:%02d:%02d", clockdata1.hour, clockdata1.min, clockdata1.sec);
  396.                             SetWindowTitles(w, t_string, NULL);
  397.  
  398.                             /**
  399.                             *** Save the actual time to s:.mbwatch
  400.                             **/
  401.                             if (wantlog == 1) {
  402.                                 foo++;
  403.                                 if (foo >= logtime*2) {
  404.                                     FILE *file = fopen("s:.mbwatch","w");
  405.                                     if (file) {
  406.                                         fprintf(file,"%s",t_string);
  407.                                         fclose(file);
  408.                                         foo = 0;
  409.                                     }
  410.                                 }
  411.                             }
  412.  
  413.                             /**
  414.                             *** Alarm
  415.                             **/
  416.                             if (alarm != 0) {
  417.                                 alarmtimer++;
  418.                                 if (alarmtimer >= alarm*2) {
  419.                                     if (soundfile) {
  420.                                         struct SoundInfo *sound;
  421.                                         sound = (struct SoundInfo *)LoadIff(soundfile);
  422.                                         if (sound) {
  423.                                             PlayIff(sound,64,L0,-35,0,1,0,0,1);
  424.                                             StopIff(L0);
  425.                                             RemoveIff(sound);
  426.                                             alarmtimer = 0;
  427.                                         }
  428.                                         else {
  429.                                             DisplayBeep(NULL);
  430.                                             alarmtimer = 0;
  431.                                         }
  432.                                     }
  433.                                     else {
  434.                                         DisplayBeep(NULL);
  435.                                         alarmtimer = 0;
  436.                                     }
  437.                                 }
  438.                             }
  439.                         }
  440.  
  441.                         if (!done)
  442.                             outReq += SendTimerIO (TR_ADDREQUEST, 0L);
  443.                     }
  444.                 }
  445.             }
  446.             DisposeTimerIO (0);
  447.         }
  448.         CloseWindow(w);
  449.     }
  450. }
  451. ///
  452.  
  453. /** The End **/
  454.